(function (mui, $) {
var page  = window.page = {};
var shares = {};

//1:为可分销  2:为已分销
var _Data = {'token':app.store('token'), 'page':0, 'nums':20, 'status':'1'};

page.refresh = function () {
	call_on_pull_down();
}

function next (cb) {
	app.log('请求参数：', mui.extend({}, _Data, {'page':_Data.page + 1, 'data':{'is_action':_Data.status}}));
	if (plus.webview.currentWebview().isVisible()) { plus.nativeUI.showWaiting(); }
	$.ajax({
		'dataType' : 'json',
		'type'     : 'post',
		'url'      : app.url('App/Goods/Distribution/lists'),
		'data'     : mui.extend({}, _Data, {'page':_Data.page + 1, 'data':{'is_action':_Data.status}}),
	})
	.fail(function (res) {
		// 失败回调
		plus.nativeUI.closeWaiting();
		app.log('获取分销商品列表失败：', res);
		app.toast('获取分销商品列表失败');
		app.tryResetPullLoading();
	})
	.done(function (res) {
		// 成功回调
		plus.nativeUI.closeWaiting();
		app.log('分销商品：', res);
		
		if (500 == res.code) { app.toast('你还未登录'); app.tryResetPullLoading(); return; }
		if (300 == res.code) { app.toast(res.msg); return; }
		if (200 != res.code) { app.toast('请求失败'); return; }
		//if (res.msg && plus.webview.currentWebview().isVisible()) { app.toast(res.msg); }
		
		if (res.data && res.data.length) { _Data.page++; }
		cb && cb(res);
	})
	;
}
// 下拉刷新
function call_on_pull_down () {
	var pullrefresh = mui('.container-pull-refresh').pullRefresh();
	_Data.page = 0;
	$(pullrefresh.element).find('.mui-scroll>.scroll-data').empty();
	
	next (function (res) {
		var dom = $(pullrefresh.element).find('.mui-scroll>.scroll-data');
		$(dom).append(app.tpl($(dom).attr('data-tpl'), res));
		pullrefresh.endPulldownToRefresh();
	});
}
// 上拉加载
function call_on_pull_up () {
	var pullrefresh = mui('.container-pull-refresh').pullRefresh();
	
	next (function (res) {
		var dom = $(pullrefresh.element).find('.mui-scroll>.scroll-data');
		var ele = app.tpl($(dom).attr('data-tpl'), res);
		if ($(ele).hasClass('no-data')) {
			if (0 == _Data.page) { $(dom).empty(); } else { $(ele).remove(); ele = null; }
		}
		$(dom).append(ele);
		pullrefresh.endPullupToRefresh();
	});
}
mui.init({
	pullRefresh   : {
		container : '.container-pull-refresh',
		down      : {callback : call_on_pull_down},
		up        : {callback : call_on_pull_up},
	}
});

mui.plusReady(function() {
	var view = plus.webview.currentWebview();
	_Data.status = app.getQueryString('status');
	setTimeout(page.refresh, mui.os.ios ? 300 : 1000);
	
	if ('2' == _Data.status) { view.hide(); }
	
});

// 去商品详情页面分享商品
$('body').delegate('.ally-product', 'tap', function (e) {
	var dom = this, view = plus.webview.currentWebview();
	var data = {
		'id' : $(dom).attr('data-id'),
	};
	app.open('product.detail.html', data);
});

function report (id) {
	$.ajax({
		'dataType' : 'json',
		'type'     : 'post',
		'url'      : app.url('App/Goods/Distribution/success'),
		'data'     : {'token':app.store('token'), 'data':{'goods_id':id}},
	})
	.fail(function (res) {
		// 失败回调
		app.log('上报失败：', res);
	})
	.done(function (res) {
		// 成功回调
		app.log('上报分享：', res);
	})
	;
}

// 分享
function _doShareMessage (share, ex, dom) {
	var view = plus.webview.currentWebview();
	var member = app.store('member');
	var m = member ? ('&uid='+member.uid+'&invitation='+member.invitation) : '';
	var message = {
		'extra'   : {'scene': ex},
		'href'    : app.link.ally + 'ally=1&id=' + $(dom).attr('data-id') + m,
		'title'   : $(dom).find('.name').text(),
		'content' : $(dom).find('.name').text(),
	};
	if (~share.id.indexOf('weibo')) {
		message.content += "；体验地址：" + message.href;
	}
	message.thumbs = ["../images/logo.png"];
	share.send(message, function() {
		// 分销上报
		report($(dom).attr('data-id'));
		if ('1' == _Data.status) { $(dom).find('button').remove(); }
		
		app.log('分享到"' + share.description + '"成功！ ');
		app.toast('分享成功');
	}, function(e) {
		app.log('分享失败：', e);
		app.toast('分享失败');
	});
}
// 分享商品
$('body').delegate('.btn-share', 'tap', function (e) {
	var dom = $(this).closest('.ally-product'); e.stopPropagation();
	var member = app.store('member');
	var m = member ? ('&uid='+member.uid+'&invitation='+member.invitation) : '';
	var data = {
  	title: $(dom).find('.name').text(),
  	desc: $(dom).find('.name').text(),
  	link: app.link.ally + '?ally=1&id=' + $(dom).attr('data-id') + m,
  	imgUrl: $(dom).find('img').attr('src'),
  	type:'link',
  	dataUrl: ''
  };
  top.window.ally.share(data);
});

})(mui, $)
